from flask import Flask, request, make_response, session, render_template, url_for
from werkzeug.utils import redirect

from Base.BaseSetings import USERNAME, PASSWORD

app = Flask(__name__)
app.secret_key = 'aaaaasssssoooommmm'

@app.route('/',methods = ['GET','POST'])
def index():
    username = session.get('username')
    if username == USERNAME:
        return app.send_static_file('Result.html')
    else:
        return redirect(url_for('login'))


@app.route('/login',methods =['GET','POST'])
def login():
    if request.method == 'GET':
        return render_template('index/login.html')
    else:
        #POST 说明提交用户名和密码
        # 接受用户传过来的用户名和密码
        uesername = request.form.get('username')
        #判断数据库有没有该用户
        if uesername == USERNAME:
            # 有用户，判断密码是否正确
            password = request.form.get('password')
            # 提示登陆成功返回首页
            if password == PASSWORD:
                session['username'] = uesername
                #返回一个跳转页面，跳转到index
                return redirect(url_for('index'))

            else:
                return render_template('index/login.html',errmsg = '密码错误')

        # 没有查到用户，提示用户不存在
        else:
            return render_template('index/login.html', errmsg='用户不存在')


if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5005,debug=True)